<template>
  <div class="tab-bar-item" @click="itemClick()">
    <slot v-if="!isActive" name="item-icon"></slot>
    <slot v-else name="item-icon-active"></slot>
    <!-- slot 会被替换, slot的 class 属性会被抹除 -->
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>
<script>
  export default {
    name: "TabBarItem",
    props: {
      link: String,
      activeColor: {
        type: String,
        default: "red"
      }
    },
    data: () => ({
    }),
    computed: {
      isActive() { // 动态决定当前逐渐是否激活
        return this.$route.path.indexOf(this.link) >= 0;
      },
      activeStyle() {
        return this.isActive ? {color: this.activeColor} : null;
      }
    },
    methods: {
      itemClick() {
        if (this.isActive) {
          return;
        }
        this.$router.replace(this.link);
      }
    }
  }
</script>
<style scoped>
  @import '../../assets/css/base.css';

  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
  }

  .tab-bar-item img {
    width: 24px;
    height: 24px;
    vertical-align: middle;
    margin: 4px 0 2px 0;
  }

  /*.active {*/
  /*  color: red;*/
  /*}*/
</style>
